-- ~~| Libraries |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local DiesalGUI 	= LibStub('DiesalGUI-1.0')
local DiesalStyle = LibStub("DiesalStyle-1.0")
local DiesalTools = LibStub('DiesalTools-1.0')
-- ~~| Lua Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local tostring										= tostring
local format 										= string.format
-- ~~| WoW Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~| ConfigToggle |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local Type 		= 'OPThotkey'
local Version 	= 1
-- ~~| ConfigToggle StyleSheets |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local styleSheet = {	
	['name-color'] = {		
		type			= 'Font',		
		color			= 'bfbfbf',		
	},
	['name-color'] = {		
		type			= 'Font',		
		color			= 'bfbfbf',		
	},	
}
local dropdownStyleSheet = {
	['frame-background'] = {			
		type			= 'texture',
		layer			= 'BACKGROUND',									
		color			= '523700',		
		offset		= 0,		
	},
	['frame-arrow'] = {
		type			= 'texture',
		layer			= 'BORDER',
		alpha 		= 0,
	},
	['text-color'] = {
		type			= 'Font',
		color			= 'ffea00',
	},
}
-- ~~| ConfigToggle Locals |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-- ~~| ConfigToggle Methods |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local methods = {
	['OnAcquire'] = function(self)		
		self:AddStyleSheet(styleSheet)
		-- self:AddStyleSheet(wireFrameSheet)
		self:Show()
		self.checkBox:Hide()		
	end,
	['OnRelease'] = function(self)

	end,
	['ApplySettings'] = function(self)			
		self:SetHeight(18)	
		if self.settings.position == 1 then
			self:SetPoint('TOPLEFT',0,-self.settings.padTop) 
			self:SetPoint('RIGHT')			
		else			
			self:SetPoint('TOPLEFT',self.settings.parentObject.children[self.settings.position-1].frame,'BOTTOMLEFT',0,0)
			self:SetPoint('RIGHT')		
		end		
		
		self.name:SetText(self.settings.optionTable.name)				
		if self.settings.optionTable.toggle ~= false then self.checkBox:Show() end			
	end,	
	['SetDB'] = function(self,db,moduleDB)				
		self.settings.db 			= db
		self.settings.moduleDB 	= moduleDB		
		
		if self.settings.optionTable.toggle ~= false then
			self.checkBox:SetChecked(db.enable)
			if self.settings.optionTable.set then self.settings.optionTable.set('toggle',db.enable) end
			self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'toggle',db.enable)		
		end
		self.hotkey:SetValueTable(db.value)	
		self.strictKeys:SetChecked(db.value.strict)	
		if self.settings.optionTable.set then self.settings.optionTable.set('hotkey',db.value) end	
		self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'hotkey',db.value)	
	end,
	['Lock'] = function(self,lock)		
		self.checkBox:Disable()	
		self.strictKeys:Disable()		
		self.hotkey:EnableMouse(false)		
	end,
	['Unlock'] = function(self,lock)				
		self.checkBox:Enable()	
		self.strictKeys:Enable()		
		self.hotkey:EnableMouse(true)		
	end,		
}
-- ~~| ConfigToggle Constructor |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local function Constructor()
	local self 		= DiesalGUI:CreateObjectBase(Type)
	local frame		= CreateFrame('Frame',nil,UIParent)		
	self.frame		= frame	
	-- ~~ Default Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	self.defaults = {	}
	-- ~~ Registered Events ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	-- ~~ Construct ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	local checkBox = DiesalGUI:Create('CheckBox')
	checkBox:SetParent(frame)	
	checkBox:SetPoint("TOPLEFT",2,-3)	
	checkBox:SetEventListener('OnValueChanged', function(this,event,value)
		self.settings.db.enable = value		
		if self.settings.optionTable.set then self.settings.optionTable.set('toggle',value)	end
		self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'toggle',value)		
	end)			
	checkBox:SetEventListener('OnEnter', function(this)
		if not self.settings.moduleDB.tooltips then return end
		GameTooltip:SetOwner(checkBox.frame, "ANCHOR_TOPLEFT",0,2) 		
		GameTooltip:AddLine('enable option')
		GameTooltip:Show()
	end)
	checkBox:SetEventListener('OnLeave', function(this)
		GameTooltip:Hide()
	end)
	
	local hotkey = DiesalGUI:Create('Dropdown') 
	hotkey:AddStyleSheet(dropdownStyleSheet)
	hotkey:SetParent(frame)
	hotkey:SetPoint('TOPRIGHT',-3,-1)
	hotkey.text:SetPoint("BOTTOMRIGHT", -4, -1)
	hotkey.text:SetJustifyH("CENTER")
	hotkey:SetSettings({	multiSelect = true, width = 50},true)		
	hotkey:SetList({ la="LA", ra="RA", ls="LS", rs="RS", lc="LC", rc="RC" },{ "la","ra","ls","rs","lc","rc" })	
	hotkey:SetEventListener('OnValueSelected', function(this,event,key,value,selectionTable)
		selectionTable.strict = self.settings.db.value.strict		
		self.settings.db.value = selectionTable		
								
		if self.settings.optionTable.set then self.settings.optionTable.set('hotkey',selectionTable) end
		self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'hotkey',selectionTable)					
	end)
	hotkey:SetEventListener('OnEnter', function(this)			
		if not self.settings.moduleDB.tooltips then return end
		GameTooltip:SetOwner(hotkey.frame, "ANCHOR_TOPRIGHT",0,2)
		GameTooltip:AddLine('|cff00aaffHotkey')
		GameTooltip:AddLine('|cff00aaffLS: |rLeft SHIFT |cff00aaffRS: |rRight SHIFT')
		GameTooltip:AddLine('|cff00aaffLC: |rLeft CTRL  |cff00aaffRC: |rRight CTRL')
		GameTooltip:AddLine('|cff00aaffLA: |rLeft ALT    |cff00aaffRA: |rRight ALT')
		GameTooltip:AddLine('|cff00aaffno selection: |rautomatic use')
		GameTooltip:Show()
	end)
	hotkey:SetEventListener('OnLeave', function(this)			
		GameTooltip:Hide()
	end)
	
	local strictKeys = DiesalGUI:Create('CheckBox')
	strictKeys:SetParent(frame)	
	strictKeys:SetPoint("TOPRIGHT",hotkey.frame,'TOPLEFT',-1,-2)
	strictKeys:SetEventListener('OnValueChanged', function(this,event,value)						
		self.settings.db.value.strict = value or false			
		if self.settings.optionTable.set then			
			self.settings.optionTable.set('hotkey',self.settings.db.value)
		end
		self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'hotkey',self.settings.db.value)		
	end)			
	strictKeys:SetEventListener('OnEnter', function(this)
		if not self.settings.moduleDB.tooltips then return end
		GameTooltip:SetOwner(strictKeys.frame, "ANCHOR_TOPLEFT",0,2) 
		GameTooltip:AddLine('|cff00aaffEnabled: |rStrict Mode')			
		GameTooltip:AddLine('In this mode only the selected combination of modifier keys will trigger the action',1,1,1,1)
		GameTooltip:AddLine('|cff00aaffDisabled: |rLazy Mode')			
		GameTooltip:AddLine('In this mode the selected combination of modifier keys will trigger the action regardless of other modifier keys held down',1,1,1,1)
		GameTooltip:Show()
	end)
	strictKeys:SetEventListener('OnLeave', function(this)
		GameTooltip:Hide()
	end)		
	
	local nameFrame = self:CreateRegion("Frame", 'nameFrame', frame)
	nameFrame:SetHeight(14)
	nameFrame:SetPoint("TOPLEFT",16,-2)	
	nameFrame:SetPoint("RIGHT",hotkey.frame,'LEFT',-14,-2)
	nameFrame:SetScript('OnEnter', function(this)		
		if not self.settings.moduleDB.configTooltips then return end
		if not self.settings.optionTable.tooltip then return end
		GameTooltip:SetOwner(nameFrame, "ANCHOR_TOPLEFT",0,2) 		
		GameTooltip:AddLine('|cffff7f00'..self.settings.optionTable.tooltip,nil,nil,nil,1)
		GameTooltip:Show()
	end)
	nameFrame:SetScript('OnLeave', function(this)		
		GameTooltip:Hide()
	end)
	
	local name = self:CreateRegion("FontString", 'name', nameFrame)
	name:SetPoint("TOPLEFT",0,-1)
	name:SetPoint("BOTTOMRIGHT",4,-2)
	name:SetJustifyV("TOP")		
	name:SetJustifyH("LEFT")
	name:SetWordWrap(false)	
	-- ~~ Frames ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~	
	self.checkBox 	= checkBox
	self.hotkey		= hotkey
	self.strictKeys= strictKeys
	-- ~~ Methods ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	for method, func in pairs(methods) do
		self[method] = func
	end	
	-- ~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	DiesalGUI:RegisterObject(self)
	return self
end
DiesalGUI:RegisterObjectConstructor(Type,Constructor,Version)
